package gui.properties;

import gui.EditorWindowSettings;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;



public abstract class FlowPanel extends JPanel{
	protected List<Component> objects;
	protected final JPanel prPanel = new JPanel();
	protected JButton button = new JButton(); 
	private final int buttonHeight = 20;
	private String name;
	
	
	public FlowPanel(String name, List<Component> list) {
		
		this.objects = list;
		this.name = name;
		Dimension buttonSize = new Dimension(EditorWindowSettings.propertiesWidth,this.buttonHeight);
		button.setPreferredSize(buttonSize);
		button.setText("(+)" + name);
		
		this.add(button);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setPrVisible(!prPanel.isVisible());				
				
			}
			
		});
		this.setLayout(new ListLayout());
		prPanel.setLayout(new BoxLayout(prPanel, BoxLayout.Y_AXIS));
		this.add(prPanel);
		setPanelComponents(list);
		this.setPrVisible(false);
		
	}
	
	public FlowPanel(String name) {
		
		//this.objects = list;
		this.name = name;
		Dimension buttonSize = new Dimension(EditorWindowSettings.propertiesWidth,this.buttonHeight);
		button.setPreferredSize(buttonSize);
		button.setText("(+)" + name);
		
		this.add(button);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setPrVisible(!prPanel.isVisible());				
				
			}
			
		});
		this.setLayout(new ListLayout());
		prPanel.setLayout(new BoxLayout(prPanel, BoxLayout.Y_AXIS));
		this.add(prPanel);
		
		this.setPrVisible(false);
		
	}

	public void setPanelComponents(List<Component> list) {		
		prPanel.removeAll();
		//objects.clear();
		this.objects = list;
		for(java.awt.Component c:objects) {
			JPanel temp = new JPanel();
			temp.setLayout(new BoxLayout(temp,BoxLayout.X_AXIS));
			temp.add(new JLabel("||--- "));
			temp.add(c);
			prPanel.add(temp);
		}
	}
	
	protected void setButtonName(String name) {
		this.name = name;
		this.button.setText((this.prPanel.isVisible()? "(-)" : "(+)") + name);
	}
	
	public void setPrVisible(boolean visible) {
		prPanel.setVisible(visible);
		
		if(visible) { 
			button.setText("(-)" + name);
			this.setPreferredSize(new Dimension(EditorWindowSettings.propertiesWidth,(int)( this.getPreferredSize().getHeight() +prPanel.getPreferredSize().getHeight())));
		}
		else {
			button.setText("(+)" + name);
			this.setPreferredSize(new Dimension(EditorWindowSettings.propertiesWidth, button.getPreferredSize().height));
		}
	}
	
	public void updateData() {
		
	}
	
	
	
}
